package org.jeecg.modules.Region.service.impl;

import org.jeecg.modules.Region.entity.Region;
import org.jeecg.modules.Region.entity.RegionPerson;
import org.jeecg.modules.Region.mapper.RegionMapper;
import org.jeecg.modules.Region.mapper.RegionPersonMapper;
import org.jeecg.modules.Region.service.IRegionService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 区域管理
 * @Author: jeecg-boot
 * @Date:   2023-02-18
 * @Version: V1.0
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

	@Autowired
	private RegionMapper regionMapper;
	@Autowired
	private RegionPersonMapper ttRegionPersonMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Region region, List<RegionPerson> ttRegionPersonList) {
		regionMapper.insert(region);
		if(ttRegionPersonList!=null && ttRegionPersonList.size()>0) {
			for(RegionPerson entity:ttRegionPersonList) {
				//外键设置
				entity.setRegionId(region.getId());
				ttRegionPersonMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Region region,List<RegionPerson> ttRegionPersonList) {
		regionMapper.updateById(region);
		
		//1.先删除子表数据
		ttRegionPersonMapper.deleteByMainId(region.getId());
		
		//2.子表数据重新插入
		if(ttRegionPersonList!=null && ttRegionPersonList.size()>0) {
			for(RegionPerson entity:ttRegionPersonList) {
				//外键设置
				entity.setRegionId(region.getId());
				ttRegionPersonMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		ttRegionPersonMapper.deleteByMainId(id);
		regionMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			ttRegionPersonMapper.deleteByMainId(id.toString());
			regionMapper.deleteById(id);
		}
	}
	
}
